package com.suyang.syt.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suyang.syt.dao.interfaces.WeixinMenuDao;
import com.suyang.syt.dao.interfaces.WeixinMsgReplyDao;
import com.suyang.syt.model.WeixinMenu;
import com.suyang.syt.model.WeixinMsgReply;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.WeixinMenuDTO;
import com.suyang.syt.service.interfaces.WeixinMenuService;
import com.suyang.syt.util.EmptyUtil;
import com.suyang.syt.util.MyException;

@Service
public class WeixinMenuServiceImpl implements WeixinMenuService {

	@Autowired
	private WeixinMenuDao weixinMenuDao;
	@Autowired
	private WeixinMsgReplyDao weixinMsgReplyDao;

	@Override
	public List<WeixinMenuDTO> addAndCreate() throws Exception {
		List<WeixinMenuDTO> datas = null;
		List<WeixinMenu> list = weixinMenuDao.list("from WeixinMenu where pid=0 ");
		if (EmptyUtil.isNotEmpty(list)) {
			if (list.size() > 3) {
				throw new MyException("微信主菜单不能超过三个!");
			}
			datas = new ArrayList<WeixinMenuDTO>();
			for (WeixinMenu weixinMenu : list) {
				datas.add(listChildren(weixinMenu));
			}
		} else {
			throw new MyException("微信主菜单为空,不能创建!");
		}
		return datas;
	}

	/** 递归调用得到所有子节点 */
	private WeixinMenuDTO listChildren(WeixinMenu weixinMenu) {
		if (weixinMenu != null) {
			WeixinMenuDTO weixinMenuDTO = new WeixinMenuDTO(weixinMenu);
			List<WeixinMenu> children = weixinMenuDao.list(" from WeixinMenu where pid=? ", weixinMenu.getId());
			List<WeixinMenuDTO> temp = new ArrayList<WeixinMenuDTO>();
			for (WeixinMenu menu : children) {
				temp.add(listChildren(menu));
			}
			if (EmptyUtil.isEmpty(temp) && weixinMenu.getPid() == 0) {
				throw new MyException("微信主菜单中不能包含空的子菜单,请检查!");
			}
			weixinMenuDTO.setSub_button(temp);
			return weixinMenuDTO;
		}
		return null;
	}

	@Override
	public List<WeixinMenuDTO> listTreegrid(long id) throws Exception {
		List<WeixinMenu> list = weixinMenuDao.list(" from WeixinMenu where pid=? ", id);
		List<WeixinMenuDTO> temp = new ArrayList<WeixinMenuDTO>();
		WeixinMenuDTO weixinMenuDTO = null;
		WeixinMsgReply weixinMsgReply = null;
		if (EmptyUtil.isNotEmpty(list)) {
			for (WeixinMenu weixinMenu : list) {
				weixinMenuDTO = new WeixinMenuDTO(weixinMenu);
				System.out.println(weixinMenu.getType());
				System.out.println(weixinMenu.getMsgReplyId());
				if (weixinMenu.getMsgReplyId() > 0 && "click".equals(weixinMenu.getType())) {
					weixinMsgReply = weixinMsgReplyDao.load(weixinMenu.getMsgReplyId());
					if (weixinMsgReply != null) {
						weixinMenuDTO.setMsgReplyType(weixinMsgReply.getMsgType());
						if (EmptyUtil.isNotEmpty(weixinMsgReply.getContent())) {
							weixinMenuDTO.setMsgReplyText(weixinMsgReply.getContent());
						}
					}
				}
				temp.add(weixinMenuDTO);
			}
		}
		return temp;
	}

	private WeixinMenu setWeixinMenu(WeixinMenu weixinMenu, WeixinMenuDTO weixinMenuDTO, boolean isAdd) {
		weixinMenu.setPid(weixinMenuDTO.getPid());
		weixinMenu.setName(weixinMenuDTO.getName());
		weixinMenu.setType(weixinMenuDTO.getType());
		weixinMenu.setUrl(weixinMenuDTO.getUrl());
		weixinMenu.setMsgReplyId(weixinMenuDTO.getMsgReplyId());
		if (isAdd) {
			weixinMenu.setCreateName(weixinMenuDTO.getCreateName());
			weixinMenu.setCreateTime(weixinMenuDTO.getCreateTime());
			if (!"view".equals(weixinMenuDTO.getType())) {
				weixinMenu.setKey(weixinMenuDTO.getType() + "_" + System.currentTimeMillis());
			}
			weixinMenu.setState("open");
		} else {
			weixinMenu.setModifyName(weixinMenuDTO.getModifyName());
			weixinMenu.setModifyTime(weixinMenuDTO.getModifyTime());
		}
		return weixinMenu;
	}

	@Override
	public AjaxObj add(WeixinMenuDTO weixinMenuDTO) throws Exception {
		AjaxObj ajaxObj = validation(weixinMenuDTO);
		if (ajaxObj != null && ajaxObj.getResult() == 1) {
			WeixinMenu weixinMenu = new WeixinMenu();
			weixinMenuDao.add(setWeixinMenu(weixinMenu, weixinMenuDTO, true));
			return new AjaxObj(1, "微信菜单增加成功!");
		}
		return ajaxObj;
	}

	private AjaxObj validation(WeixinMenuDTO weixinMenuDTO) {
		if (weixinMenuDTO == null) {
			return new AjaxObj(0, "参数错误");
		}
		if (weixinMenuDTO.getPid() == 0) {
			long count = (Long) weixinMenuDao.queryByHql(" select count(*) from WeixinMenu where pid=0 ");
			if (count >= 3) {
				return new AjaxObj(0, "一级菜单最多为三个!");
			} else {
				if (weixinMenuDTO.getName().length() > 4) {
					return new AjaxObj(0, "一级菜单名字长度最大为4个汉字");
				}
			}
		} else {
			if (EmptyUtil.isEmpty(weixinMenuDTO.getType()) || weixinMenuDTO.getType().equals("weixinMenu")) {
				return new AjaxObj(0, "二级菜单类型不能是主菜单类型!");
			}
			long count = (Long) weixinMenuDao.queryByHql(" select count(*) from WeixinMenu where pid=? ", weixinMenuDTO.getPid());
			if (count >= 5) {
				return new AjaxObj(0, "二级菜单最多为五个!");
			} else {
				if (weixinMenuDTO.getName().length() > 7) {
					return new AjaxObj(0, "二级菜单名字长度最大为7个汉字");
				}
				WeixinMenu parent = weixinMenuDao.load(weixinMenuDTO.getPid());
				if (parent == null) {
					return new AjaxObj(0, "父菜单加载错误,请联系管理员!");
				}
				if (parent.getPid() != 0) {
					return new AjaxObj(0, "目前微信系统只能支持二级子菜单!");
				}
				parent.setState("closed");
				weixinMenuDao.update(parent);
			}
		}
		String type = weixinMenuDTO.getType();
		if (EmptyUtil.isEmpty(type)) {
			return new AjaxObj(0, "菜单类型不能为空!");
		}
		if (type.equals("click")) {
			if (weixinMenuDTO.getMsgReplyId() <= 0) {
				return new AjaxObj(0, "菜单click事件必须输入正确的回复消息ID!");
			}
		} else if (type.equals("view")) {
			if (EmptyUtil.isEmpty(weixinMenuDTO.getUrl())) {
				return new AjaxObj(0, "菜单view事件必须输入正确的URL地址!");
			}
		}
		return new AjaxObj();
	}

	@Override
	public AjaxObj deletes(String ids) throws Exception {
		if (EmptyUtil.isNotEmpty(ids)) {
			String[] idss = ids.split(",");
			if (idss != null && idss.length > 0) {
				for (String id : idss) {
					delete(Long.parseLong(id));
				}
				return new AjaxObj(1, "删除成功！");
			}
		} else {
			return new AjaxObj(0, "请选择要删除的记录！");
		}
		return new AjaxObj(0, "删除失败！");
	}

	private void delete(long id) throws Exception {
		if (id > 0) {
			List<WeixinMenu> list = weixinMenuDao.list(" from WeixinMenu where pid=? ", id);
			if (EmptyUtil.isNotEmpty(list)) {
				for (WeixinMenu weixinMenu : list) {
					delete(weixinMenu.getId());
				}
			}
			weixinMenuDao.delete(id);
		}
	}

	@Override
	public AjaxObj update(WeixinMenuDTO weixinMenuDTO) throws Exception {
		AjaxObj ajaxObj = validation(weixinMenuDTO);
		if (ajaxObj != null && ajaxObj.getResult() == 1) {
			WeixinMenu weixinMenu = weixinMenuDao.load(weixinMenuDTO.getId());
			if (weixinMenu == null) {
				return new AjaxObj(0, "加载微信菜单信息失败,请联系管理员!");
			}
			weixinMenuDao.update(setWeixinMenu(weixinMenu, weixinMenuDTO, false));
			return new AjaxObj(1, " 更新成功");
		}
		return ajaxObj;
	}

}
